package dhm.com.dhmshop.updata.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.Bean;
import dhm.com.dhmshop.entity.InvoiceBean;
import dhm.com.dhmshop.utils.SpUtils;

public class InvoiceaddActivity extends BaseActiity implements LoginContract.IView {

    @BindView(R.id.group)
    RadioGroup group;
    @BindView(R.id.invoiceHead)
    EditText invoiceHead;
    @BindView(R.id.invoiceCode)
    EditText invoiceCode;
    @BindView(R.id.invoiceAddr)
    EditText invoiceAddr;
    @BindView(R.id.invoicePhoneNumber)
    EditText invoicePhoneNumber;
    @BindView(R.id.invoiceBankName)
    EditText invoiceBankName;
    @BindView(R.id.invoiceBankNo)
    EditText invoiceBankNo;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.add)
    LinearLayout add;

    private PressenterImpl pressenter;
    private String type;
    private InvoiceBean.DataBean bean;

    @Override
    protected int getLayout() {
        return R.layout.activity_invoiceadd;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);
        type = getIntent().getStringExtra("type");
        if (type.equals("1")) {
            bean = (InvoiceBean.DataBean) getIntent().getSerializableExtra("bean");
            invoiceHead.setText(bean.getInvoiceHead());
            invoiceCode.setText(bean.getInvoiceCode());
            if (bean.getInvoiceType() == 1) {
                invoiceAddr.setText(bean.getInvoiceAddr());
                invoiceBankName.setText(bean.getInvoiceBankName());
                invoiceBankNo.setText(bean.getInvoiceBankNo());
                invoicePhoneNumber.setText(bean.getInvoicePhoneNumber());
                group.check(R.id.invoiceType);
                add.setVisibility(View.VISIBLE);
                title.setText("修改发票");
            } else {
                group.check(R.id.invoiceType_normal);
                add.setVisibility(View.GONE);
                title.setText("新增发票");
            }
        }
    }

    @Override
    protected void initData() {
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (group.getCheckedRadioButtonId()) {
                    case R.id.invoiceType:
                        add.setVisibility(View.VISIBLE);
                        break;
                    case R.id.invoiceType_normal:
                        add.setVisibility(View.GONE);
                        break;
                    default:
                }

            }
        });
    }

    @OnClick({R.id.back, R.id.edit_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.edit_login:
                String head = invoiceHead.getText().toString();
                String addr = invoiceAddr.getText().toString();
                String name = invoiceBankName.getText().toString();
                String bankno = invoiceBankNo.getText().toString();
                String code = invoiceCode.getText().toString();
                String phone = invoicePhoneNumber.getText().toString();
                if (head==null||head.equals("")){
                    Toast.makeText(this, "请填写发票抬头", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (code==null||code.equals("")){
                    Toast.makeText(this, "请填公司税号", Toast.LENGTH_SHORT).show();
                    return;
                }
                String uid = SpUtils.getString(this, "uid");
                Map<String,String> map=new HashMap<>();
                map.put("token",Constant.TOKEN);
                map.put("uid", uid);
                map.put("invoiceHead", head);
                map.put("invoiceCode", code);
                map.put("invoiceType", "0");
                switch (group.getCheckedRadioButtonId()) {
                    case R.id.invoiceType:
                        if (addr==null||addr.equals("")){
                            Toast.makeText(this, "请填注册地址", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (phone==null||phone.equals("")){
                            Toast.makeText(this, "请填注册电话", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (name==null||name.equals("")){
                            Toast.makeText(this, "请填开户银行", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (bankno==null||bankno.equals("")){
                            Toast.makeText(this, "请填银行账号", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        map.put("invoiceType", "1");
                        map.put("invoiceAddr", addr);
                        map.put("invoicePhoneNumber", phone);
                        map.put("invoiceBankName", name);
                        map.put("invoiceBankNo", bankno);
                        break;
                    default:
                }
                if (type.equals("1")){
                    map.put("id",bean.getId()+"");
                    pressenter.sendMessage(this, Constant.InvoiceEdit,map, Bean.class);
                }else {
                    pressenter.sendMessage(this, Constant.InvoiceAdd,map, Bean.class);
                }
                break;
            default:
        }
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof Bean){
            Bean bean= (Bean) data;
            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
            if (bean.getCode()==1){
                finish();
            }

        }
    }

    @Override
    public void fail(String error) {

    }
}
